function arrayToObject(a) {
    var o = {};
    for (var i = 0; i < a.length; i++) {
        o[a[i].itemName] = '';
    }
    return o;
}


function intersect(arr1, arr2) {
    var r = [], o = {}, l = arr2.length, ii, vv;
        for (ii = 0; ii < l; ii++) {
            o[arr2[ii]] = true;
        }
    l = arr1.length;
    for (ii = 0; ii < l; ii++) {
        vv = arr1[ii];
        if (vv in o) {
            r.push(vv);
        }
    }
    return r;
}

function getDeniedWorkflowNames() {
    logger.log("getDeniedWorkflowNames");
    var deniedWfNames = [], deniedWfNamesG = [], deniedWfNamesU = [],
    connector = remote.connect("alfresco"),
    result = connector.get("/api/people/" + user.name + "?groups=true");

    if (result.status == 200) {
        var groups = eval('(' + result + ')').groups,
        grantwf = config.scoped["Workflow"]["grant-workflows"].childrenMap["workflow"];
        if (grantwf) {
            for (var i = 0, il = grantwf.size(); i < il; i++) {
                if (grantwf.get(i).attributes["grant-type"] == "group") {
                    var groupsAllowed = grantwf.get(i).attributes["grant-name"].split(",");
                    var isGroupAllowed = false;
                    for (var j = 0; j < groupsAllowed.length; j++) {
                        if (groupsAllowed[j] === "ALL" || groupsAllowed[j] === "all") {
                            isGroupAllowed = true;
                            break;
                        } else if (groupsAllowed[j] in arrayToObject(groups)) {
                            isGroupAllowed = true;
                            break;
                        }
                    }
                    if (isGroupAllowed) {
                        continue;
                    }
                    deniedWfNamesG.push(grantwf.get(i).attributes["name"]);
				} else if (grantwf.get(i).attributes["grant-type"] == "user") {
					var usersAllowed = grantwf.get(i).attributes["grant-name"].split(",");
                    var isUserAllowed = false;
                    for (var q = 0; q < usersAllowed.length; q++) {
                        if (usersAllowed[q] === "ALL" || usersAllowed[q] === "all") {
                            isUserAllowed = true;
                            break;
                        } else if (usersAllowed[q] == user.name) {
                            isUserAllowed = true;
                            break;
                        }
                    }
                    if (isUserAllowed) {
                        continue;
                    }
                    deniedWfNamesU.push(grantwf.get(i).attributes["name"]);
				}
			}
		}
		deniedWfNames = intersect(deniedWfNamesG,deniedWfNamesU);
		return deniedWfNames;
	} else {
		logger.log("Error getting groups");
	}
	return [];
}


function getHiddenWorkflowNames() {
	var hiddenWorkflowNames = [],
		hiddenWorkflows = config.scoped["Workflow"]["hidden-workflows"].childrenMap["workflow"];
	if (hiddenWorkflows) {
		for (var hi = 0, hil = hiddenWorkflows.size(); hi < hil; hi++) {
			hiddenWorkflowNames.push(hiddenWorkflows.get(hi).attributes["name"]);
		}
	}
	var deniedWorkflowNames = getDeniedWorkflowNames();
	for each(var el in deniedWorkflowNames) {
		hiddenWorkflowNames.push(el)
	};
	return hiddenWorkflowNames;
}

model.hiddenWorkflowNames = getHiddenWorkflowNames();
model.maxItems = 25;

function main()
{
    // Widget instantiation metadata...
    var myWorkflows = {
    id : "MyWorkflows",
    name : "Alfresco.dashlet.MyWorkflows",
    options : {
        hiddenWorkflowNames : model.hiddenWorkflowNames,
        maxItems : parseInt(model.maxItems),
        regionId : args['region-id']
    }
};

var dashletResizer = {
    id : "DashletResizer",
    name : "Alfresco.widget.DashletResizer",
    initArgs : ["\"" + args.htmlid + "\"", "\"" + instance.object.id + "\""],
    useMessages: false
};

var dashletTitleBarActions = {
    id : "DashletTitleBarActions",
    name : "Alfresco.widget.DashletTitleBarActions",
    useMessages : false,
    options : {
        actions: [
        {
            cssClass: "help",
            bubbleOnClick:
            {
                message: msg.get("dashlet.help")
            },
            tooltip:  msg.get("dashlet.help.tooltip")
        }]
    }
};

model.widgets = [myWorkflows, dashletResizer, dashletTitleBarActions];

}

main();
